package com.ins.service.impl;


import com.ins.common.enums.ClassifyEnum;
import com.ins.common.utils.Json;
import com.ins.common.utils.TimeAgoUtils;
import com.ins.mapper.AppInfoMarketMapper;
import com.ins.mapper.AppResMapper;
import com.ins.model.entity.AppInfoMarket;
import com.ins.model.entity.AppRes;
import com.ins.model.query.AppInfoMarketQuery;
import com.ins.model.vo.AppInfoMarketVO;
import com.ins.model.vo.RecommendInfoVO;
import com.ins.model.vo.RecommendVO;
import com.ins.service.AppInfoMarketService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.ins.model.entity.table.AppInfoMarketTableDef.APP_INFO_MARKET;

/**
 * 信息集市 服务层实现。
 *
 * @author ins
 * @since 2024-05-14
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AppInfoMarketServiceImpl extends ServiceImpl<AppInfoMarketMapper, AppInfoMarket> implements AppInfoMarketService {


    private final AppResMapper appResMapper;

    @Override
    public RecommendVO recommend() {
        // 轮播广告
        List<RecommendInfoVO> carousel = mapper.randRecommend(null, 4);
        // 摄影摄像师
        List<RecommendInfoVO> camera = mapper.randRecommend(ClassifyEnum.CAMERA_MAN.name(), 6);
        //演员
        List<RecommendInfoVO> performe = mapper.randRecommend(ClassifyEnum.PERFORMER.name(), 6);
        // 场景
        List<RecommendInfoVO> scene = mapper.randRecommend(ClassifyEnum.SCENE.name(), 6);

        Set<Long> ids = Stream.of(carousel, camera, performe, scene).flatMap(List::stream).peek(a -> {
            a.setAgoTimes(TimeAgoUtils.format(a.getCreateTime()));
            List<String> picList = Json.toList(a.getPic(), String.class);
            a.setPic(picList.get(0));
        }).map(RecommendInfoVO::getId).collect(Collectors.toSet());

        UpdateChain.of(AppInfoMarket.class).set(APP_INFO_MARKET.REMAINING_NUM, APP_INFO_MARKET.REMAINING_NUM.subtract(1))
                   .where(APP_INFO_MARKET.ID.in(ids)).update();

        return new RecommendVO(carousel, camera, performe, scene);
    }

    @Override
    public Page<AppInfoMarketVO> page(AppInfoMarketQuery query) {
        return this
                .page(Page.of(query.getPage(), query.getLimit()), APP_INFO_MARKET.CLASSIFY.eq(query.getClassify(), StrUtil.isNotBlank(query.getClassify())))
                .map(a -> {
                    AppInfoMarketVO vo = new AppInfoMarketVO();
                    BeanUtil.copyProperties(a, vo);
                    vo.setPic(a.getPic().get(0));
                    vo.setAgoTimes(TimeAgoUtils.format(a.getCreateTime()));
                    return vo;
                });

    }

    @Override
    public AppInfoMarketVO getInfo(Long id) {

        AppInfoMarket aim = getById(id);
        AppInfoMarketVO appInfoMarketVO = new AppInfoMarketVO();
        BeanUtil.copyProperties(aim, appInfoMarketVO);
        List<AppRes> appRes = appResMapper.selectListByIds(aim.getPortfolio());
        appInfoMarketVO.setSwiperList(appRes);
        return appInfoMarketVO;
    }
}